package com.cswnode.Service.impl;

import com.cswnode.Dao.DiscussPostMapper;
import com.cswnode.Entity.DiscussPost;
import com.cswnode.Service.DiscussPostService;
import com.cswnode.Util.SensitiveUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @author csw
 * @create 2020/9/22 13:29
 */
@Service
public class DiscussPostServiceImpl implements DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private SensitiveUtil sensitiveUtil;

    public List<DiscussPost> findDiscussPost(int userId, int offset, int limit){
        return discussPostMapper.selectDiscussPost(userId,offset,limit);
    }

    public int findDiscussPostRow(int userId){
        return discussPostMapper.selsectDiscussPostRows(userId);
    }

    @Override
    public int insertDiscussPost(DiscussPost post) {
        if (post == null){
            throw new RuntimeException("参数不能为空！");
        }
        //将特殊字符转义，避免用户输入特殊字符，破坏网页
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));
        //屏蔽敏感词
        post.setTitle(sensitiveUtil.filter(post.getTitle()));
        post.setContent(sensitiveUtil.filter(post.getContent()));
        return discussPostMapper.insertDiscussPost(post);
    }

    @Override
    public DiscussPost findDiscussPostById(int id) {
        return discussPostMapper.selectDiscussPostById(id);
    }

    @Override
    public int updateCommentCount(int id, int CommentCount) {
        return discussPostMapper.updateCountById(id,CommentCount);
    }
}
